<template>
	<view>
		<uni-search-bar :radius='100' cancelButton='none' @input="input"></uni-search-bar>
		<!-- 搜索建议结果项 -->
		<view class="sugg-list" v-if="searchList.length>0">
			<view class="sugg-item" v-for="(item,index) in searchList" :key='index' @click="goToGoodsDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history" v-else>
			<!-- 标题 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clearhistory"></uni-icons>
			</view>
			<!-- 历史结果展示 -->
			<view class="history-list">
				<uni-tag type="success" :text='item'  v-for="(item,index) in historyList" :key='index' @click="goToGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null, //定时器
				keyword: '', //搜索关键字
				searchList: [],
				historyList: JSON.parse(uni.getStorageSync('keyword'))||[]
			};
		},
	/* 利用计算属性翻转数组 使得最新搜索的记录在最前
		computed:{
			historys(){
				return [...this.historyList].reverse()
			}
		}, */
		methods: {
			input(e) {
				clearTimeout(this.timer) //清除定时器
				// 防抖设置
				this.timer = setTimeout(() => {
					this.keyword = e
					this.getSearchList()
				}, 500)
			},
			async getSearchList() {
				// 判断关键字是否为空
				if (this.keyword == '') {
					// 清空搜索列表数据
					this.searchList = [];
					return
				}
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/goods/qsearch', {
					query: this.keyword
				})
				if (meta.status !== 200) {
					return uni.$showMsg()
				}
				this.searchList = message
				// console.log(message)
				this.saveKeyword()
			},
			goToGoodsDetail(item) {
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
				})
			},
			// 保存关键字为历史记录
		saveKeyword(){
			// 数组去重
			const set=new Set(this.historyList)
			set.delete(this.keyword)//如果set中有与keyword相同的先删除
			set.add(this.keyword)//将数据加入set对象中
			this.historyList=Array.from(set)//将set对象转换为数组
			this.historyList=[...this.historyList].reverse()
			// 将历史记录存储到本地
			uni.setStorageSync('keyword',JSON.stringify(this.historyList))
		},
		// 清空历史记录
		clearhistory(){
			this.historyList=[]
			uni.setStorageSync('keyword','[]')
		},
		goToGoodsList(key){
			uni.navigateTo({
				url:`/subpkg/goods_list/goods_list?query=${key}`
			})
		}
		}
	}
</script>

<style lang="scss">
	.sugg-list {
		padding: 0 5px;

		.sugg-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 12px;
			border-bottom: 1px solid #efefef;
			padding: 13px 0;

			.goods-name {
				overflow: hidden;
				white-space: nowrap;
				text-overflow: ellipsis;
			}
		}
	}

	.history {
		padding: 0 5px;

		.history-title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 40px;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}

		.history-list {
			display: flex;
			uni-tag{
				margin: 5px 5px 0 0;
			}
		}
	}
</style>
